package com.library.admin.listener;

import books.borrowing.entity.SubscriberEntity;

import com.library.admin.AdmLibrary;
import com.library.admin.list.RentTable;
import com.library.admin.manager.RentManagerPanel;
import com.library.admin.service.LibraryService;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class FindRentBySubscriberListener implements ClickListener {
	private static final long serialVersionUID = 4451508558575882831L;
	
	private RentManagerPanel rentManagerPanel;
	
	public FindRentBySubscriberListener(RentManagerPanel rentManagerPanel) {
		this.rentManagerPanel = rentManagerPanel;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) rentManagerPanel.getApplication();
		String subscriberLogin = rentManagerPanel.getSubscriberLogin().getValue().toString();
		
		if (rentManagerPanel.getComponentCount() == 3) {
			rentManagerPanel.removeComponent(rentManagerPanel.getComponent(2));
		}
		try {
			SubscriberEntity subscriber = LibraryService.getServiceSubscriber().findSubscriberByLogin(subscriberLogin);
//			BookDto book = LibraryService.getServiceBook().findBook("T3L3PHONE");
//			LibraryService.getServiceBorrowing().rentBook(subscriber.getLogin(), book.getIsbn());
			rentManagerPanel.addComponent(new RentTable(subscriber.getLogin()));
		}
		catch (Exception e) {
			libraryApplication.getMainWindow().showNotification("Aucun abonné trouvé avec login '" + subscriberLogin + "'",
					Notification.TYPE_ERROR_MESSAGE);
			e.printStackTrace();
		}
	}
}
